# import jsonpath
# data={'name':'tom','msg':{'age':18,'name':'汤姆'}}
# str1=str(data)
# print(str1)
# print(type(str1))
# #获取字典字一行的某个key的值，找不到返回false
# res=jsonpath.jsonpath(data,'$.name')
# # print(res)
# # print(type(res))
# # res2=jsonpath.jsonpath(data,"$..name")
# # print(res2)
# # print(type(res2))
# # res3=jsonpath.jsonpath(data,'$..age')
# # print(res3)
# # print(type(res3))
# if '18' in str1:
#     print('ok')
# else:
#     print('get out')

import jsonpath


class PublicAssert():
    def __init__(self, dic, res):
        self.dic = eval(dic['expect'])
        self.res = res

    def public_assert(self):
        assert self.res.status_code in [200, 201, 304], f'状态码不正确：{self.res.status_code}'
        msg = ""
        for k, v in self.dic.items():
            res_dict = self.res.json()
            real = jsonpath.jsonpath(res_dict, f'$..{k}')
            if real == False:
                msg += f'\n在相应结果内，未找到预期结果字段{k}'
            else:
                if str(v) != str(real[0]):
                    msg += f'\n断言失败，预期结果{str(v)}与实际结果{str(real[0])}不一致'
        assert msg == "", msg


if __name__ == '__main__':
    from common.readData import ReadData
    from common.configHttp import ConfigHttp

    rd = ReadData()
    rd_data = rd.read_excel(1)
    ch = ConfigHttp(rd_data[0])
    res = ch.run()
    # 调试当前的类
    pa = PublicAssert(rd_data[0], res)
    pa.public_assert()
